// CSE 142 Winter 2008, Marty Stepp // // This program reads a file and adds all its numeric tokens. import java.io.*; // for File import java.util.*; // for Scanner public class Echo { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("numbers.txt")); double sum = 0.0; while (input.hasNext()) { // if the next token is a real number, // read it and add it to the sum if (input.hasNextDouble()) { double number = input.nextDouble(); System.out.println("number = " + number); sum += number; } else { // next token is not a number String junk = input.next(); System.out.println("Bad token: " + junk); } } System.out.println("Sum is " + sum); } }